package ro.sergiu.finder.utils;

import java.util.Collection;
import java.util.Iterator;

/**
 *
 * @author Toporjinschi
 */
public final class StringUtils {

    public static interface Predicator<R, P> {

        public R perform(P parameter);
    }

    private StringUtils() {
    }

    public static boolean isBlank(String value) {
        return value == null || value.trim().isEmpty();
    }

    public static boolean isNOTBlank(String value) {
        return !isBlank(value);
    }

    public static <P> String joinBeanItem(Collection<P> collection, String separator, Predicator<String, P> predicator) {
        Iterator<P> it = collection.iterator();
        StringBuilder sb = new StringBuilder(25);
        while (it.hasNext()) {
            String value = predicator.perform(it.next());
            if (isBlank(value)) {
                continue;
            }
            sb.append(separator);
            sb.append(value);
        }
        String rez = sb.toString();
        if (isNOTBlank(rez)) {
            return rez.substring(separator.length());
        }
        return "";
    }
}
